import { observable, action, useStrict, computed, asMap, toJS } from 'mobx';
import { SelectPage,AtSomeone } from 'weaver-mobile-page';
import BaseStore from './BaseStore';
import base from '../util/base';
import * as API_REQ from '../apis/form';
import { _ } from '../util/pcPublic';

const initState = {
    remindInfo: {},
    remindTypeStore: null,
    reminderStore: null,
    baseParam: {},
    callbackFn: null,
    operators: null,
    checkedReminder: ""
}
export class RemindStore extends BaseStore{

    @observable remindInfo;    //提醒信息
    @observable remindTypeStore;
    @observable reminderStore;
    baseParam;
    callbackFn;
    operators;
    @observable checkedReminder;

    constructor(){
        super();
        this.changeState(initState);
    }
    @observable defaultRemindSet = '';
    @action('初始化提醒信息')
    doInit = (obj={}) =>{
        const value = obj.value;
        this.remindInfo = {...obj, ...{submitType:value, forwardType:value}};
        this.defaultRemindSet = value; //设置提醒的默认选中项
        this.changeRemindType(value, true);     //转发提交参数给默认值
        this.remindTypeStore = new SelectPage.Store({id:"remindType", route:"/req"});
        this.reminderStore = new AtSomeone.Store({id:"operators", route:"/req/reminderConfirm"});
    }

    @action('修改提醒信息')
    changeRemindType = (type, isForward) =>{
        const changeTypeInfo = isForward ? {forwardType:type} : {submitType:type};
        this.remindInfo = {...this.remindInfo, ...changeTypeInfo};
        const { wfOperateStore,wfSignInputStore } = base.allStores;
        if(isForward)
            wfSignInputStore.setForwardParams({remindTypes:type});
        else
            wfOperateStore.changeSubmitParam({remindTypes:type});
    }

    @action('获取确认提醒人信息')
    loadRemindData = (location={}) =>{
        const { params,callback } = location;
        this.baseParam = params;
        this.callbackFn = callback;
        API_REQ.getRemindData({...this.baseParam}).then(datas =>{
            const { operators } = datas;
            this.operators = operators;
            this.changeReminder(operators);
        });
    }

    @action('改变提醒人')
    changeReminder = (datas=[]) =>{
        let reminder = "";
        datas.map(item =>{
            reminder += "," + item.id;
        });
        if(reminder !== "")
            reminder = reminder.substring(1);
        this.checkedReminder = reminder;
    }

    @action('触发提醒')
    triggerRemind = () =>{
        const reqParams = {...this.baseParam, ...{remindTypes: this.remindInfo.value, reminder:this.checkedReminder}};
        API_REQ.doRemind(reqParams).then(datas =>{
            if(typeof this.callbackFn === "function")
                this.callbackFn();
        });
    }
}